#!/bin/bash
#../../cmake/check_update.sh ${CMAKE_CURRENT_SOURCE_DIR}/resources ${CMAKE_CURRENT_BINARY_DIR}/myresources_update_tmp || rm ${CMAKE_CURRENT_BINARY_DIR}/myresources.c
  
FILE=$1

tmp="$2"
if [ ! -e $tmp ]; then
	touch $tmp
fi

if [ -f $FILE ]; then
	now_time=`stat -c %Y $FILE`
elif [ -d $FILE ]; then
	now_time=$(find "$FILE" -type f | xargs stat -c %Y | sort -nr | head -n 1)
else
	echo "no file no dir, exit 2"
	exit 2
fi

#echo "now time is $now_time"

last_time=`cat $tmp`
echo "now_time  is $now_time"
echo "last_time is $last_time"

if [[ "$now_time" -gt "$last_time" ]]; then  
    echo "Source file is newer than target file"  
    echo "$now_time" > $tmp
    rm $3 || true
else  
    echo "Target file is up to date"  
fi
